jQuery.gestures = {};
(function ($) {
	var canvas;

	var recording=false;
	var previous = {x:0,y:0};
	var chain = [];

	var color;
	var onGestureComplete;

	function get_last_move (previous, cur) {
		var diff = {x:cur.x-previous.x, y : cur.y-previous.y};
		var mv = null;
		if (Math.abs(diff.x) > Math.abs(diff.y)) {
			if (diff.x <= -10) mv = 'L';
			if (diff.x >= 10)  mv = 'R';
		} else {
			if (diff.y <= -10) mv = 'U';
			if (diff.y >= 10)  mv = 'D';
		}
		return mv;
	}

	function drawline(previous,cur) {
		var context = document.getElementById('gestures_canvas').getContext('2d');
		context.strokeStyle = color;
		context.beginPath();
		context.moveTo(previous.x,previous.y);
		context.lineTo(cur.x,cur.y);
		context.closePath();
		context.stroke();
	}

	function setup() {
		$.gestures.Recording=false;
		chain=[];
		$(canvas).hide();
	}	


	$.gestures.init = function(ob) {
		ob = $.extend(ob, {color:'#FF0000', onGestureComplete: SendGesturesToBackground});
		color = ob.color;
		$.gestures.OnGestureComplete = ob.onGestureComplete;

		// add the canvas to the body element
		$('body').append('<canvas id="gestures_canvas"/>');
		canvas = $('#gestures_canvas');
		$(window).resize(setup);
		
		setup();

		// capturing mouse gestures
		$(document).mousedown(function(e) {
			if (e.button == 2) {
				$(canvas).css({ position:'fixed', top:0, left:0, 'z-index':9999, })
					.attr({ height: $(window).height(), width: $(window).width() })
					.show();
				$.gestures.Recording=true;
				previous={x:e.clientX,y:e.clientY}
				$(document).bind('mousemove.gestures', mouseMoving).bind('mouseup.gestures', mouseUp);		 
			}
		});

		function mouseMoving(e) {
			if($.gestures.Recording) {
				var pos = {x:e.clientX,y:e.clientY};
				drawline(previous, pos);
				var mv = get_last_move(previous, pos);
				if (mv != null) {
					if (chain[chain.length-1] != mv) {
						chain.push(mv);
					}
				}
				previous={x:e.clientX,y:e.clientY}
			}
		}

		function mouseUp(e) {  
			if($.gestures.Recording) {
				var c = chain;
				$.gestures.OnGestureComplete.call(this, chain);
				$(document).unbind('mousemove.gestures').unbind('mouseup.gestures');
				if (chain.length > 0)
				{
					$(this)[0].oncontextmenu = function() {
						return false;
					}
				}				
				setup();				
			}
		}

	}
	
	$.gestures.OnGestureComplete = onGestureComplete;
	$.gestures.Recording = recording;
})(jQuery)

function GoBack() {
	history.go(-1);
}

function GoForward() {
	history.go(1);
}

function SendGesturesToBackground(chain) {
	chrome.extension.sendRequest({gestures:chain.join('')}, function(response) {});
}

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    if (request.action == "goBack")
      GoBack();
    else if (request.action == "goForward")
      GoForward();
});

$.gestures.init({color:'#ff0000'});

//for access from the background page
var gestureRef = $.gestures;